package com.htfyun.ft.agingtest;

import android.content.Context;

import com.htfyun.ft.FTApplication;
import com.htfyun.ft.R;
import com.htfyun.ft.config.NVRomKeysAttr;
import com.htfyun.ft.device.DeviceAttr;

public enum AgingHardwareItemAttr {
//    key(R.string.ft_item_key, false),
    led(R.string.ft_item_led, true, true, NVRomKeysAttr.led_breath),
    wifi(R.string.ft_item_wifi, true, true, NVRomKeysAttr.wifi),
    bt(R.string.ft_item_bluetooth, true, true, NVRomKeysAttr.bluetooth),
    speaker(R.string.ft_item_speaker, true, true, NVRomKeysAttr.speaker),
    recorder(R.string.ft_item_mic, true, true, NVRomKeysAttr.mic_6_channel),

    battery(R.string.ft_item_battery, false, true, NVRomKeysAttr.battery),

    proximity_sensor(R.string.ft_item_proximity_sensor, false, true, NVRomKeysAttr.proximity_sensor),
    light_sensor(R.string.ft_item_light_sensor, false, true, NVRomKeysAttr.light_sensor),
    camera(R.string.ft_item_camera, false, true, NVRomKeysAttr.camera),

    emmc(R.string.ft_item_emmc, true, true, NVRomKeysAttr.aging_emmc),
    cpu(R.string.ft_item_cpu, true, true, NVRomKeysAttr.aging_cpu),
    servo(R.string.ft_item_servo, true, true, NVRomKeysAttr.servo),
    ;

    private final int nameRes;
    private final boolean showCount;
    private final boolean enableTest;
    private final NVRomKeysAttr nvRomKeysAttr;


    private AgingHardwareItemAttr(int nameRes, boolean showCount, boolean enableTest,
                            NVRomKeysAttr att) {
        this.nameRes = nameRes;
        this.showCount = showCount;
        this.enableTest = enableTest;
        nvRomKeysAttr = att;
    }

    private String getName() {
        final Context context = FTApplication.getContext();

        return context.getString(nameRes);
    }

    public boolean isEnableTest() {
        if (!enableTest) {
            return false;
        }

        return DeviceAttr.isNVRomKeySupported(nvRomKeysAttr);
    }

    private String keyCount() {
        return this.name() + "-count";
    }
    private String keyErrorLog() {
        return this.name() + "-errorLog";
    }

    private String keyInfo() {
        return this.name() + "-info";
    }

    public void setInfo(String info) {
        AgingSharedPreference.setString(keyInfo(), info);
    }

    public String getInfo() {
        return AgingSharedPreference.getString(keyInfo());
    }


    public void setErrorLog(String errorLog) {
        AgingSharedPreference.setString(keyErrorLog(), errorLog);
    }

    public String getErrorLog() {
        return AgingSharedPreference.getString(keyErrorLog());
    }


    public void setTestCount(int count) {
        AgingSharedPreference.setInt(keyCount(), count);
    }

    public int getTestCount() {
        return AgingSharedPreference.getInt(keyCount(), 0);
    }

    public void addOneTestCount() {
        int cnt = getTestCount();
        setTestCount(cnt + 1);
    }
    public void clearTestCount() {
        setTestCount(0);
    }

    public void clear() {
        clearTestCount();
        setErrorLog("");
        setInfo("");
    }


    public String getItemCountString() {
        if (!showCount) {
            return "";
        }
        final Context context = FTApplication.getContext();
        int count = getTestCount();
        return getName() + " : " + context.getString(R.string.aging_item_count, count);
    }

}
